﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace OBNEPGGrabber
{
    static class ChannelParser
    {
        private static IStatusUpdate m_IUpdate = null;

        // VERY dirty and volatile code to parse the channel file list
        // some aspects are hard-coded, such as the fact that when an SD and HD
        // version of a channel is available, only the HD channel will
        // end up in the list.
        public static void Parse(DirectoryInfo diFolder, string strFileName, ref List<Channel> lstChannel, IStatusUpdate IUpdate)
        {
            m_IUpdate = IUpdate;
            bool bLast = false;

            try
            {
                string text = System.IO.File.ReadAllText(diFolder.ToString() + "\\" + strFileName);
                int idx = text.IndexOf("H.tv");
                idx = text.IndexOf("e.push(\"", idx);

                do
                {
                    int idxNext = 0;
                    int iRemove = 0;
                    text = text.Remove(0, idx + 8);
                    idx = text.IndexOf("\"");

                    Channel channel = new Channel();
                    channel.Name = text.Substring(0, idx);

                    idx = text.IndexOf("\"default\":");
                    text = text.Remove(0, idx + 11);
                    idx = text.IndexOf("\"");
                    channel.FriendlyName = text.Substring(0, idx);
                    text = text.Remove(0, idx);

                    //First find next entry
                    idxNext = text.IndexOf("e.push(\"", 0);
                    if (idxNext == -1)
                        bLast = true;

                    idx = text.IndexOf("if(Z.hd&&t==\"ghm\"");
                    iRemove = 16;
                    if (idx > idxNext)
                    {
                        idx = text.IndexOf("if((c.demo||c.hd", 0);
                        iRemove = 16;
                    }
                    if (idx==-1 || idx > idxNext)
                    {
                        idx = text.IndexOf("if(Z.hd&&(t==\"ghm\"");
                        iRemove = 17;
                    }
                    if (idx != -1 && idx < idxNext)
                    {
                        text = text.Remove(0, idx + iRemove);
                        idx = text.IndexOf("b.e=\"");
                        text = text.Remove(0, idx + 5);
                        idx = text.IndexOf("\"");
                        channel.HDURL = text.Substring(0, idx).Replace("igmp", "udp");
                        if (channel.HDURL.Contains(";"))
                        {
                            channel.HDURL = channel.HDURL.Substring(0, channel.HDURL.IndexOf(";"));
                        }
                    }
                    idxNext = text.IndexOf("e.push(\"", 0);
                    idx = text.IndexOf("if(t==\"ghm\"");
                    iRemove = 11;
                    if (idx > idxNext)
                    {
                        idx = text.IndexOf("if(t==\n\"ghm\"");
                        iRemove = 12;
                    }
                    if (idx != -1 && idx < idxNext)
                    {
                        text = text.Remove(0, idx + iRemove);
                        idx = text.IndexOf("b.e=\"");
                        text = text.Remove(0, idx + 5);
                        idx = text.IndexOf("\"");
                        channel.SDURL = text.Substring(0, idx).Replace("igmp", "udp");
                        if (channel.SDURL.Contains(";"))
                        {
                            channel.SDURL = channel.SDURL.Substring(0, channel.SDURL.IndexOf(";"));
                        }
                    }

                    lstChannel.Add(channel);
                    //First find next entry
                    idx = text.IndexOf("e.push(\"", 0);
                } while (!bLast);
                m_IUpdate.UpdateStatus("Parsed channel list file", false);
            }
            catch (Exception)
            {
                m_IUpdate.UpdateStatus("Error parsing channel file", true);
            }
        }
    }
}
